package com.qcby.shujia.demo.controller;

import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.qcby.shujia.demo.common.web.ResultData;
import com.qcby.shujia.demo.entity.SMSParameter;
import com.qcby.shujia.demo.entity.User;
import com.qcby.shujia.demo.entity.vo.UserVo;
import com.qcby.shujia.demo.service.UserService;
import com.qcby.shujia.demo.util.ContextUtil;
import com.qcby.shujia.demo.util.TokenUtil;
import com.qcby.shujia.demo.util.VerCodeGenerateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.http.HTTPException;
import java.io.IOException;
import java.util.List;

/**
 * @className: LoginController
 * @description: 登陆控制
 * @author: whz
 * @date: 2021/9/23
 **/
@RestController
@Api(tags = {"login模块"})
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserService userService;
    private SMSParameter smsParameter;
    @GetMapping
    @ApiOperation("login验证接口")
    public ResultData login(User user){
        //登录判断
        if(StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())) {
            return ResultData.error("账号或密码为空");
        }
       User userDb= userService.login(user);
        if(userDb==null){
            return ResultData.error("账号或密码错误");
        }
        //根据登录用户获取所有权限信息
        List<String> authStrList=userService.getAuthStrListByUser((UserVo) userDb);
        //服务器存入用户登录信息之前设置其访问权限
        userDb.setAuthStrList(authStrList);
        //登录成功
        String token= TokenUtil.getToken(userDb);
        userDb.setToken(token);
        return ResultData.ok(userDb);

    }
    @GetMapping("sendText")
    public void sendText(String phone) throws HTTPException, IOException, com.github.qcloudsms.httpclient.HTTPException {
        // 生成 随机验证码
        String ver = VerCodeGenerateUtil.generateVerCode();
        // 这里对应你的短信模板，如果需要传递两个参数，那么字符串数组应该有两个值
        String[] code = new String[]{ver};
       smsParameter = new SMSParameter();
        //获得 id 和 key
        SmsSingleSender sender = new SmsSingleSender(this.smsParameter.getAppId(), this.smsParameter.getAppKey());
//        SmsSingleSenderResult result = sender.sendWithParam("86", phone, smsParameter.getTemplateId(),
//               code, smsParameter.getSmsSign(), "", "" );
        //发送短信，86 指的是国内短信，第二个是模板的 id，第三个是传递的参数，第四个是签名内容，后面两个可以传递空
        SmsSingleSenderResult result = sender.sendWithParam("86", phone, this.smsParameter.getTemplateId(),
                code, this.smsParameter.getSmsSign(), "", "");
        // 如果 result 的结果是 0，说明发送成功
        if (result.result == 0) {
            System.out.println(code[0]);
        } else {// 如果结果不是 0，说明出错了，打印错误信息以帮助修改
            System.out.println(result.errMsg);
        }
    }

}
